﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;

namespace ServerMonitor.Common
{
    public class NetWorkHelper
    {
        /// <summary>
        /// 查看Ip对应的服务器是否可用
        /// </summary>
        /// <param name="Ip">Ip地址</param>
        /// <param name="pingCount">尝试Ping次数</param>
        /// <returns></returns>
        public static bool Pinging(string Ip, int pingCount)
        {
            bool online = false; //是否在线
            Ping ping = new Ping();
            while (pingCount > 0)
            {
                try
                {
                    PingReply pingReply = ping.Send(Ip);
                    if (pingReply.Status == IPStatus.Success)
                    {
                        online = true;
                        break;
                    }
                    else
                    {
                        online = false;
                        pingCount--;
                    }
                }
                catch (Exception ex)
                {
                    online = false;
                    pingCount--;
                }
            }
            return online;
        }


    }
}